package eu.liquify.ui {
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.utils.Dictionary;
	import no.doomsday.console.ConsoleUtil;
	public class MultiTabBox extends Box {
		public var tabs:HBox;
		public var tabBox:VBox;
		public var contentPanel:ScrollablePanel;
		private var allItems:Vector.<DisplayObject> = new Vector.<DisplayObject>();
		private var tabMap:Dictionary = new Dictionary();
		private var setSelected:Boolean;
		public function MultiTabBox() {
			fixedSize = true;
			fitToGrid = false;
			removeRawChild(content);
			var comps:DisplayObject = Layout.parse(this,
				<comps>
					<vbox id="tabBox">
						<hbox id="tabs" fixedSize="false" fitToGrid="false" spacing="0" />
						<scrollablepanel id="contentPanel" background="false" padding="0" fixedSize="true" />
					</vbox>
				</comps>
			);
			//tabBox.addChild(content);
			contentPanel.addChild(content);
			container = comps as DisplayObjectContainer;
			addRawChild(container);
		}
		
		override public function resize():void {
			super.resize();
			contentPanel.width = width;
			contentPanel.height = height;
			contentPanel.resize();
		}
		
		override public function addChild(child:DisplayObject):DisplayObject {
			super.addChild(child);
			
			allItems.push(child);
			
			var tab:MultiTabButton = new MultiTabButton();
			tab.label = getLabel(child);
			tab.name = tab.label;
			tabMap[child] = tab;
			tab.addEventListener(MouseEvent.MOUSE_DOWN, tabDown);
			tabs.addChild(tab);
			
			redrawTabs();
			
			return child;
		}
		override public function removeChild(child:DisplayObject):DisplayObject {
			if (items.indexOf(child) > -1) super.removeChild(child);
			
			allItems.splice(allItems.indexOf(child), 1);
			
			var tab:MultiTabButton = tabMap[child];
			tab.removeEventListener(MouseEvent.MOUSE_DOWN, tabDown);
			tabs.removeChild(tab);
			delete tabMap[child];
			redrawTabs();
			
			contentPanel.resize();
			
			return child;
		}
		
		
		private function getLabel(child:DisplayObject):String {
			if (child is UIItem) return (child as UIItem).label;
			return child.name;
		}
		
		private function redrawTabs():void {
			for (var i:int = 0; i < tabs.numChildren; i++) {
				var tab:MultiTabButton = tabs.getChildAt(i) as MultiTabButton;
				tab.style = i == 0 ? MultiTabButton.LEFT : (i == tabs.numChildren-1 ? MultiTabButton.RIGHT : MultiTabButton.MIDDLE);
				//tab.resize();
			}
			tabs.resize();
		}
		
		private function tabDown(e:MouseEvent):void {
			stage.addEventListener(MouseEvent.MOUSE_UP, tabUp);
			var tabClicked:Button = e.currentTarget as Button;
			if (!e.shiftKey && !e.ctrlKey) {
				for (var i:int = 0; i < tabs.numChildren; i++) {
					var tab:Button = tabs.getChildAt(i) as Button;
					tab.selected = false;
				}
				setSelected = true;
			} else {
				setSelected = !tabClicked.selected;
			}
			tabClicked.selected = setSelected;
			addTabListeners();
			updateVisible();
		}
		private function tabUp(e:MouseEvent):void {
			stage.removeEventListener(MouseEvent.MOUSE_UP, tabUp);
			removeTabListeners();
		}
		
		private function addTabListeners():void {
			for (var i:int = 0; i < tabs.numChildren; i++) {
				var tab:Button = tabs.getChildAt(i) as Button;
				tab.addEventListener(MouseEvent.ROLL_OVER, tabOver);
			}
		}
		private function removeTabListeners():void {
			for (var i:int = 0; i < tabs.numChildren; i++) {
				var tab:Button = tabs.getChildAt(i) as Button;
				tab.removeEventListener(MouseEvent.ROLL_OVER, tabOver);
			}
		}
		private function tabOver(e:MouseEvent):void {
			var tab:Button = e.currentTarget as Button;
			tab.selected = setSelected;
			updateVisible();
		}
		
		private function updateVisible():void {
			var item:DisplayObject;
			var i:int;
			for (i = 0; i < allItems.length; i++) {
				item = allItems[i];
				if (content.contains(item)) content.removeChild(item);
			}
			items.length = 0;
			for (i = 0; i < allItems.length; i++) {
				item = allItems[i];
				var tab:Button = tabs.getChildByName(getLabel(item)) as Button;
				if (tab.selected) {
					content.addChild(item);
					items.push(item);
				}
			}
			contentPanel.constrain();
			reposition();
			contentPanel.resize();
		}
		
		/*
		private function tabClick(tab:Button):void {
			var child:DisplayObject;
			if (tab.selected) {
				child = hiddenItems.getChildByName(tab.name);
				hiddenItems.removeChild(child);
				super.addChild(child);
			} else {
				child = contents.getChildByName(tab.name);
				super.removeChild(child);
				hiddenItems.addChild(child);
			}
		}
		*/
		
	}
}